package com.example.hosptial.newController;

import com.example.hosptial.model.view.DoctorView;
import com.example.hosptial.model.view.WardStatusView;
import com.example.hosptial.model.view.WardBedPatientView;
import com.example.hosptial.service.WardStatusService;
import com.example.hosptial.service.BedService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 病房状态管理控制器
 */
@Controller
@RequestMapping("/doctor/ward-status")
@RequiredArgsConstructor
public class WardStatusController {

    private final WardStatusService wardStatusService;
    private final BedService bedService;

    /**
     * 显示病房状态主页面
     */
    @GetMapping
    public String showWardStatusPage(HttpSession session, Model model) {
        DoctorView doctor = (DoctorView) session.getAttribute("doctor");
        if (doctor == null) {
            return "redirect:/doctor/auth/login";
        }
        return "doctor/ward-status";
    }

    /**
     * 获取科室所有楼层（AJAX调用）
     */
    @GetMapping("/floors")
    @ResponseBody
    public ResponseEntity<?> getFloors(HttpSession session) {
        DoctorView doctor = (DoctorView) session.getAttribute("doctor");
        if (doctor == null) {
            return ResponseEntity.status(401).body("未登录或会话已过期");
        }
        List<Integer> floors = wardStatusService.getFloorsByDeptId(doctor.getDeptId());
        return ResponseEntity.ok(floors);
    }

    /**
     * 获取指定楼层的病房状态（AJAX调用）
     */
    @GetMapping("/wards")
    @ResponseBody
    public ResponseEntity<?> getWardsByFloor(
            @RequestParam(required = false) Integer floorNum,
            HttpSession session) {
        DoctorView doctor = (DoctorView) session.getAttribute("doctor");
        if (doctor == null) {
            return ResponseEntity.status(401).body("未登录或会话已过期");
        }
        try {
            List<WardStatusView> wards;
            if (floorNum != null) {
                wards = wardStatusService.getWardsByDeptIdAndFloor(doctor.getDeptId(), floorNum);
            } else {
                wards = wardStatusService.getAllWardsByDeptId(doctor.getDeptId());
            }
            return ResponseEntity.ok(wards);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 获取科室所有病房状态（AJAX调用）
     */
    @GetMapping("/all")
    @ResponseBody
    public ResponseEntity<?> getAllWards(HttpSession session) {
        DoctorView doctor = (DoctorView) session.getAttribute("doctor");
        if (doctor == null) {
            return ResponseEntity.status(401).body("未登录或会话已过期");
        }
        try {
        List<WardStatusView> wards = wardStatusService.getAllWardsByDeptId(doctor.getDeptId());
            Map<String, Object> response = new HashMap<>();
            response.put("wards", wards);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 获取病房详细信息（AJAX调用）
     */
    @GetMapping("/detail")
    @ResponseBody
    public ResponseEntity<?> getWardDetail(
            @RequestParam Integer wardId,
            HttpSession session) {
        DoctorView doctor = (DoctorView) session.getAttribute("doctor");
        if (doctor == null) {
            return ResponseEntity.status(401).body("未登录或会话已过期");
        }
        try {
            List<WardBedPatientView> beds = wardStatusService.getWardBedDetails(wardId);
            return ResponseEntity.ok(beds);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 获取病房的护士信息（AJAX调用）
     */
    @GetMapping("/ward-nurses")
    @ResponseBody
    public ResponseEntity<?> getWardNurses(
            @RequestParam Integer wardId,
            HttpSession session) {
        DoctorView doctor = (DoctorView) session.getAttribute("doctor");
        if (doctor == null) {
            return ResponseEntity.status(401).body("未登录或会话已过期");
        }
        try {
            List<Map<String, Object>> nurses = bedService.getWardNurses(wardId);
            return ResponseEntity.ok(nurses);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
} 